package com.audi.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.audi.R;
import com.audi.dto.PlaceItem;
import com.audi.utils.Constants.PLACE_CAPABILITY;

public class ListPlaceDialogAdapter extends BaseAdapter {

    private Activity mActivity;
    private ArrayList<PlaceItem> mPlaceCardList;

    public ListPlaceDialogAdapter(Activity activity,
	    ArrayList<PlaceItem> placeCardList) {
	mActivity = activity;
	mPlaceCardList = placeCardList;
    }

    @Override
    public int getCount() {
	return mPlaceCardList.size();
    }

    @Override
    public Object getItem(int position) {
	return mPlaceCardList.get(position);
    }

    @Override
    public long getItemId(int positionListPlaceDialog) {
	return positionListPlaceDialog;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup viewgroup) {
	PlaceItem item = mPlaceCardList.get(position);
	ViewHolder viewholder = null;
	// khoi tao cho viewholder
	if (convertView == null) {
	    viewholder = new ViewHolder();
	    convertView = LayoutInflater.from(mActivity).inflate(
		    R.layout.row_place, null);
	    viewholder.placeName = (TextView) convertView
		    .findViewById(R.id.txt_place_name);
	    viewholder.placeCount = (TextView) convertView
		    .findViewById(R.id.txt_place_count);
	    viewholder.placeStatus = (TextView) convertView
		    .findViewById(R.id.txt_place_status);
	    convertView.setTag(viewholder);
	} else {
	    viewholder = (ViewHolder) convertView.getTag();
	}
	viewholder.placeName.setText(item.getPlaceName());
	viewholder.placeCount.setText("(" + item.getDeviceNumber() + ")");
	if (item.getCapabilityType() == PLACE_CAPABILITY.NONE.ordinal()) {
	    viewholder.placeStatus.setText("");
	} else if (item.getCapabilityType() == PLACE_CAPABILITY.PUBLIC
		.ordinal()) {
	    viewholder.placeStatus.setText("Public");
	} else if (item.getCapabilityType() == PLACE_CAPABILITY.PRIVATE
		.ordinal()) {
	    viewholder.placeStatus.setText("Private");
	}
	return convertView;
    }

    static class ViewHolder {
	private TextView placeName;
	private TextView placeCount;
	private TextView placeStatus;
    }
}
